package com.hpp.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hpp.model.NewsType;
import com.hpp.service.INewsTypeService;
import com.hpp.utils.DataTableModel;
import com.hpp.utils.DateUtils;
import com.hpp.utils.RespJson;
import com.hpp.utils.ResultCode;

/**
* @ClassName: DemandController  
* @Description: 新闻资讯
* @author Administrator  
* @date 2019年1月14日  
*
 */
@Controller
@RequestMapping(value = "/newstype")
public class NewsTypeController {
	
	private Logger logger = LoggerFactory.getLogger(NewsTypeController.class);
	
	@Autowired
	INewsTypeService newsTypeService;

	/**
	 * @Title getNewsList  
	 * @Description 获取新闻栏目列表
	 * @author hanpp
	 * @param title
	 * @param start
	 * @param length
	 * @param draw
	 * @return DataTableModel<?>
	 * @date 2019年1月19日 下午7:22:19  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/page/list")
	public DataTableModel<?> getNewsList(
			@RequestParam(value = "title",required = false) String title,
			int start,int length,int draw){
		DataTableModel<NewsType> result = new DataTableModel<NewsType>();
		result.setDraw(draw);
		PageHelper.startPage(start / length + 1, length);
		HashMap<String, Object> map = new HashMap<String,Object>();
		if (StringUtils.isNotBlank(title)) {
			map.put("title", title);
		}
		List<NewsType> list = newsTypeService.getNewsTypeList(map);
		PageInfo<NewsType> pageInfo = new PageInfo<NewsType>(list);
		if (pageInfo != null && pageInfo.getEndRow() != 0) {
			result.setData(pageInfo.getList());
			result.setRecordsFiltered(new Long(pageInfo.getTotal()).intValue());
			result.setRecordsTotal(new Long(pageInfo.getTotal()).intValue());
		}
		return result;
	}
	
	/**
	 * @Title getNewsType  
	 * @Description 获取栏目列表
	 * @author hanpp
	 * @return RespJson
	 * @date 2019年1月19日 下午7:36:37  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/list")
	public RespJson getNewsType(){
		try {
			HashMap<String, Object> map = new HashMap<String,Object>();
			List<NewsType> types = newsTypeService.getNewsTypeList(map);
			return new RespJson(true, "获取成功", ResultCode.SUCCESS, types);
		} catch (Exception e) {
			logger.error("获取异常：" + e);
			return new RespJson(false, "获取失败", ResultCode.ERROR, "");
		}
	}
	
	/**
	 * @Title operateNewsType  
	 * @Description 新增修改栏目类别
	 * @author hanpp
	 * @param id
	 * @param newsType
	 * @param remark
	 * @param imgPath
	 * @return RespJson
	 * @date 2019年1月19日 下午7:28:21  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/operate")
	public RespJson operateNewsType(
			@RequestParam(value = "id",required = false) Integer id,
			@RequestParam(value = "newsType",required = true) String newsType,
			@RequestParam(value = "remark",required = false) String remark,
			@RequestParam(value = "imgPath",required = false) String imgPath){
		try {
			if (null == id) {
				NewsType type = new NewsType();
				//新增栏目 默认设置文章数为零
				type.setNewsCount(0);
				type.setNewsType(newsType);
				if (StringUtils.isNotBlank(remark)) {
					type.setRemark(remark);
				}
				type.setUpdateTime(DateUtils.formatDate(new Date(), DateUtils.YMDHMS));
				type.setCreateTime(DateUtils.formatDate(new Date(), DateUtils.YMDHMS));
				newsTypeService.addNewsType(type);
				return new RespJson(true, "新增成功", ResultCode.SUCCESS, "");
			} else {
				HashMap<String, Object> map = new HashMap<String,Object>();
				map.put("id", id);
				NewsType type = newsTypeService.getNewsType(map);
				type.setNewsType(newsType);
				if (StringUtils.isNotBlank(remark)) {
					type.setRemark(remark);
				}
				type.setUpdateTime(DateUtils.formatDate(new Date(), DateUtils.YMDHMS));
				newsTypeService.updateNewsType(type);
				return new RespJson(true, "修改成功", ResultCode.SUCCESS, "");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("栏目类别操作失败" + e);
			return new RespJson(false, "操作失败", ResultCode.ERROR, "");
		}
	}
	
	/**
	 * @Title getNewsTypeInfo  
	 * @Description 获取栏目类别详情
	 * @author hanpp
	 * @param id
	 * @return RespJson
	 * @date 2019年1月19日 下午7:30:15  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/info")
	public RespJson getNewsTypeInfo(
			@RequestParam(value = "id",required = true) String id){
		try {
			HashMap<String, Object> map = new HashMap<String,Object>();
			map.put("id", id);
			NewsType type = newsTypeService.getNewsType(map);
			return new RespJson(true, "获取成功", ResultCode.SUCCESS, type);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取异常：" + e);
			return new RespJson(false, "获取失败", ResultCode.ERROR, "");
		}
	}
	
	/**
	 * @Title deleteNewsTypeInfo  
	 * @Description 删除栏目类别
	 * @author hanpp
	 * @param ids
	 * @return RespJson
	 * @date 2019年1月19日 下午7:31:09  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public RespJson deleteNewsTypeInfo(
			@RequestParam(value = "ids",required = true) String ids){
		try {
			HashMap<String, Object> map = new HashMap<String,Object>();
			map.put("list", ids.split(","));
			newsTypeService.deleteNewsType(map);
			return new RespJson(true, "删除成功", ResultCode.SUCCESS, "");
		} catch (Exception e) {
			logger.error("删除异常：" + e);
			return new RespJson(false, "删除失败", ResultCode.ERROR, "");
		}
	}
}
